//
//  yxpJsonRequest.m
//  TestProject
//
//  Created by 杨小平 on 14-8-18.
//  Copyright (c) 2014年 yangxiaoping. All rights reserved.
//

#import "yxpJsonRequest.h"

@interface yxpJsonRequest ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
    NSURLConnection *_connection;
    NSMutableData *_receiveData;
    UITextView *_textView;
}
@end

@implementation yxpJsonRequest

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 300)];
    label.center=self.view.center;
    label.textAlignment=NSTextAlignmentCenter;
    label.numberOfLines=10;
    label.text=@"     这个例子是以json格式请求PHP的例子,这里没有设置post请求头，如果需要，可以加上，但不保证会返回正确地结果";
    label.textColor=[[UIColor skyBlueColor] colorWithAlphaComponent:0.3];
    [self.view addSubview:label];
	
    NSURL *url= [NSURL URLWithString:@"http://112.124.104.241/api/user/register/"];
    
    NSMutableURLRequest *requests = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:50];
    
    [requests setHTTPMethod:@"POST"];
    
    //封装参数方法
//    //方法一
//    NSDictionary *jsonDic=[NSDictionary dictionaryWithObjectsAndKeys:@"15181752605",@"phone",@"111111",@"password",nil];
//    NSData *data;
//    
//    if ([NSJSONSerialization isValidJSONObject:jsonDic])
//    {
//        NSError *error;
//        data = [NSJSONSerialization dataWithJSONObject:jsonDic options:NSJSONWritingPrettyPrinted error:&error];
//    }
    
    //方法二
    NSString *post = @"phone=15181752605&password=111111";
    NSData *data = [post dataUsingEncoding:NSUTF8StringEncoding];
    
    [requests setHTTPBody:data];
    
    
    //异步
    _connection=[NSURLConnection connectionWithRequest:requests delegate:self];
    
    
//    //同步
//    NSURLResponse *response = nil;
//    NSError *error = nil;
//    NSData *responseData = [NSURLConnection sendSynchronousRequest:requests returningResponse:&response error:&error];
//    
//    if (error)
//    {
//        NSLog(@"%@",error);
//    }
//    else
//    {
//        //如果返回的是字符串
//        NSString *responseString = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
//        NSLog(@"string===%@",responseString);
//        
//        //返回的是json
//        NSError *jsonError = nil;
//        id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
//        NSLog(@"json==%@",json);
//        
//    }
    
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error %@",error);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSString *string=[[NSString alloc] initWithData:connection.currentRequest.HTTPBody encoding:NSUTF8StringEncoding];
    NSLog(@"=connecation==%@==response==%@",string,response);
    _receiveData=[[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //NSString *dataString=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [_receiveData appendData:data];
    id dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    //NSString *string=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",dic);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
